// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_render_frame.h"
#include "mk_render_step.h"
#include "mk_error.h"
#include "mk_render_step_view_culling.h"
#include "mk_render_step_refraction.h"
#include "mk_render_step_scene_render.h"
#include "mk_render_step_shadow_map.h"

namespace MK3DE
{
	CRenderFrame::CRenderFrame()
	{

	}

	CRenderFrame::~CRenderFrame()
	{
		RemoveAllRenderStep();
	}

	BOOL CRenderFrame::AddRenderStep(CRenderStep* renderStep)
	{
		CRenderStep* step = GetRenderStepByName(renderStep->GetName().c_str());
		if (step)
			return FALSE;

		_vecRenderStep.push_back(renderStep);
		return TRUE;
	}

	BOOL CRenderFrame::RemoveRenderStep(const char* stepName)
	{
		std::vector<CRenderStep*>::iterator i = _vecRenderStep.begin();
		while (i != _vecRenderStep.end())
		{
			CRenderStep* step = *i;
			std::string stepName = step->GetName();
			if (stepName.compare(stepName) == 0)
			{
				SAFE_DELETE(step);
				_vecRenderStep.erase(i);
				return TRUE;
			}

			++i;
		}

		return FALSE;
	}

	void CRenderFrame::RemoveAllRenderStep()
	{
		std::vector<CRenderStep*>::iterator i = _vecRenderStep.begin();
		while (i != _vecRenderStep.end())
		{
			CRenderStep* step = *i;
			SAFE_DELETE(step);

			++i;
		}

		_vecRenderStep.clear();
	}

	void CRenderFrame::Render()
	{
		std::vector<CRenderStep*>::iterator i = _vecRenderStep.begin();
		while (i != _vecRenderStep.end())
		{
			CRenderStep* step = *i;
			step->Render();

			++i;
		}
	}

	CRenderStep* CRenderFrame::GetRenderStepByName(const char *name)
	{
		std::vector<CRenderStep*>::iterator i = _vecRenderStep.begin();
		while (i != _vecRenderStep.end())
		{
			CRenderStep* step = *i;
			std::string stepName = step->GetName();
			if (stepName.compare(name) == 0)
				return step;

			++i;
		}

		return NULL;
	}

	BOOL CRenderFrame::MakeDefaultRenderSteps()
	{
		BOOL ret = FALSE;

		RemoveAllRenderStep();

		CRenderStepViewCulling* rsViewCulling = new CRenderStepViewCulling(this);
		MK_PROCESS_ERROR(rsViewCulling);

		//CRenderStepShadow* rsShadow = new CRenderStepShadow(this);
		//MK_PROCESS_ERROR(rsShadow);

		CRenderStepRefraction* rsRefraction = new CRenderStepRefraction(this);
		MK_PROCESS_ERROR(rsRefraction);

		CRenderStepSceneRender* rsSceneRender = new CRenderStepSceneRender(this);
		MK_PROCESS_ERROR(rsSceneRender);

		_vecRenderStep.push_back(rsViewCulling);
		//_vecRenderStep.push_back(rsShadow);
		_vecRenderStep.push_back(rsRefraction);
		_vecRenderStep.push_back(rsSceneRender);

		ret = TRUE;

Exit0:
		return ret;
	}
}